/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.client;

import edu.uoc.tdp.entity.User;
import edu.uoc.tdp.i18n.TDPLanguageConstants;
import edu.uoc.tdp.i18n.TDPLanguageUtils;
import edu.uoc.tdp.common.SubsystemConnect;
import edu.uoc.tdp.common.SubsystemMaintenance;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Locale;

/**
 * @author tomeu
 * @version 1.0
 */
public class Client {

    public static SubsystemMaintenance remoteMaintenance;
    public static SubsystemConnect remoteConnect;
    private final String url = new String("rmi://localhost/");
    public static User availableUser = null; // tomeu

    public Client() {
        try {
            System.out.println("Conectando al servidor desde cliente...");
            remoteMaintenance = (SubsystemMaintenance) Naming.lookup(url + "remoteMaintenance");
            remoteConnect = (SubsystemConnect) Naming.lookup(url + "remoteConnect");
            System.out.println("¡Conectado!");
        } catch (NotBoundException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (RemoteException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    private void realizarTest() {
        System.out.println("Realizando test...");
        try {
            String respuesta = remoteMaintenance.test();
            System.out.println(respuesta);
            respuesta = remoteConnect.test();
            System.out.println(respuesta);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        initialitzeApplication(args);

        Client cl = new Client();
        cl.realizarTest();

//        PanelUser pnll = null;
//        pnll = new PanelUser(Boolean.TRUE);

        MenuPrincipal pnll = null;
        pnll = new MenuPrincipal();

        pnll.setTitle(TDPLanguageUtils.getMessage(TDPLanguageConstants.CLIENT_MAIN_TITLE));
        //pnll.setLocationRelativeTo(null);
        pnll.setSize(1024, 748);
        //ppl.setDefaultCloseOperation(PanelUser.EXIT_ON_CLOSE);
        pnll.setVisible(true);
    }

    private static void initialitzeApplication(String[] args) {
        if (args.length == 0) {
            TDPLanguageUtils.setDefaultLanguage("i18n/messages");
        } else if (args.length == 1) {
            Locale locale = new Locale(args[0]);
            TDPLanguageUtils.setLanguage("i18n/messages", locale);
        }
    }
}